﻿#nullable disable

using FirstDemo.MyEnum;
using Newtonsoft.Json;
using System;
using System.ComponentModel;
using System.Reflection;

namespace FirstDemo.Models
{
    public class ResultModel
    {
        /// <summary>
        /// 响应码
        /// </summary>
        public int Code { get; set; }
        /// <summary>
        /// 消息
        /// </summary>
        public string Message { get; set; }
        /// <summary>
        /// 时间戳
        /// </summary>
        public string TimeStamp { get; set; }
        /// <summary>
        /// 返回的数据
        /// </summary>
        public object? Data { get; set; }
        /// <summary>
        /// 分页信息，若为空则Json不序列化
        /// </summary>
        [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
        public Cursor Csr { get; set; }
        /// <summary>
        /// 分页信息类
        /// </summary>
        public class Cursor
        {
            /// <summary>
            /// 总数
            /// </summary>
            public int Total { get; set; }
            /// <summary>
            /// 当前页
            /// </summary>
            public int Offset { get; set; }
            /// <summary>
            /// 每页条数
            /// </summary>
            public int Limit { get; set; }

            public Cursor(int total, int offset, int limit)
            {
                Total = total;
                Offset = offset;
                Limit = limit;
            }

        }

        /// <summary>
        /// 通过状态码默认Message返回
        /// </summary>
        /// <param name="code"></param>
        /// <param name="data"></param>
        public ResultModel(int code, object data)
        {
            Code = code;
            Message = GetDescription(((StatusCode)code));
            TimeStamp = DateTime.Now.ToString();
            Data = data;
        }

        /// <summary>
        /// 通过用户定义Message返回
        /// </summary>
        /// <param name="code"></param>
        /// <param name="message"></param>
        /// <param name="data"></param>
        public ResultModel(int code, string message, object data)
        {
            Code = code;
            Message = message;
            TimeStamp = DateTime.Now.ToString();
            Data = data;
        }

        /// <summary>
        /// 带分页信息返回
        /// </summary>
        /// <param name="code"></param>
        /// <param name="message"></param>
        /// <param name="data"></param>
        /// <param name="cursor"></param>
        public ResultModel(int code, string message, object data, Cursor cursor)
        {
            Code = code;
            Message = message;
            TimeStamp = DateTime.Now.ToString();
            Data = data;
            Csr = cursor;
        }

        public static string GetDescription(object obj)
        {
            string objName = obj.ToString();
            Type t = obj.GetType();
            FieldInfo fi = t.GetField(objName);

            DescriptionAttribute[] arrDesc = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);

            return arrDesc[0].Description;
        }
    }
}
